/*
 * @Description: 多个菜单在这里被手动添加,Orz
 * @Author: diyhome
 * @Date: 2020-08-12 16:31
 * @File: menu_string
*/

#include <limits.h>
#include "menu_func.h"
#include "menu_table.h"

menu *p_Current_Item;

uint8_t total_current_child_item = 0;         // 计算子菜单数量
uint8_t oled_current_index = 0;               // OLED上的索引


static void show_Item_Group(uint8_t start_index);               // 显示5项菜单
static void total_item();                                       // 计算当前指针共有多少个子菜单

void key_enter() {
    if (p_Current_Item->pChildMenu == NULL) return;
    p_Current_Item = p_Current_Item->pChildMenu;                // 将当前菜单项指针指向子菜单
    oled_current_index = 0;                                     // 索引归零

    total_item();                                               // 计算数组大小
    OLED_CLS();                                                 // 清屏
    show_Item_Group(0);                               // 显示数组

    OLED_ShowStr(0, oled_current_index + OLED_SHOW_LINE_START, HIGHLIGHT_SIGN, 1);
}

void key_return() {
    if (p_Current_Item->pParentMenu == NULL) return;
    p_Current_Item = p_Current_Item->pParentMenu;               // 将当前菜单项指针指向子菜单
    oled_current_index = 0;                                     // 索引归零
    total_item();                                               // 计算数组大小

    OLED_CLS();
    show_Item_Group(0);                               // 显示数组

    OLED_ShowStr(0, oled_current_index + OLED_SHOW_LINE_START, HIGHLIGHT_SIGN, 1);
}

void key_up() {
    // 菜单选项指针平移
    p_Current_Item = (p_Current_Item->item_Index == 1) ? p_Current_Item + total_current_child_item - 1 : p_Current_Item - 1;

    // 计算当前高亮显示行数的位置
    if (total_current_child_item <= OLED_SHOW_LINE_MAX)
        oled_current_index = oled_current_index == 0 ? total_current_child_item - 1 : oled_current_index - 1;
    else {
        oled_current_index = oled_current_index == 0 ? OLED_SHOW_LINE_MAX - 1 : oled_current_index - 1;
        oled_current_index = p_Current_Item->item_Index >= OLED_SHOW_LINE_STOP - 1 ? OLED_SHOW_LINE_MAX - 1 : oled_current_index;
    }

    OLED_CLS();         // 清屏

    // 滚动显示
    show_Item_Group(p_Current_Item->item_Index <= oled_current_index + OLED_SHOW_LINE_START - 1 ? 0 : p_Current_Item->item_Index - OLED_SHOW_LINE_MAX);

    // 标识当前在哪一个选项
    OLED_ShowStr(0, oled_current_index + OLED_SHOW_LINE_START, HIGHLIGHT_SIGN, 1);
    HAL_Delay(0);
}

void key_down() {
    // 菜单选项指针平移
    p_Current_Item = (p_Current_Item->item_Index == total_current_child_item) ? p_Current_Item - total_current_child_item + 1 : p_Current_Item + 1;
    OLED_CLS();

    // 为了适应铺满屏幕的菜单和未铺满屏幕的菜单做的判断
    if (total_current_child_item <= OLED_SHOW_LINE_MAX)
        oled_current_index = oled_current_index == total_current_child_item - 1 ? 0 : oled_current_index + 1;
    else
        oled_current_index = oled_current_index == OLED_SHOW_LINE_MAX - 1? oled_current_index : oled_current_index + 1;

    oled_current_index = p_Current_Item->item_Index == 1 ? 0 : oled_current_index;

    // 滚动显示
    show_Item_Group(p_Current_Item->item_Index < oled_current_index + OLED_SHOW_LINE_START ? 0 : p_Current_Item->item_Index - OLED_SHOW_LINE_MAX);

    // 标识当前在哪一个选项
    OLED_ShowStr(0, oled_current_index + OLED_SHOW_LINE_START, HIGHLIGHT_SIGN, 1);
}

void menu_init() {
    p_Current_Item = main_Menu;
    total_item();
    show_Item_Group(0);
    OLED_ShowStr(0, oled_current_index + OLED_SHOW_LINE_START, HIGHLIGHT_SIGN, 1);

}

void show_Item_Group(uint8_t start_index) {
    menu *temp_p_current = p_Current_Item + start_index + 1 - p_Current_Item->item_Index;                         // 做个备份,不想动原指针
    for (int i = 0; i < OLED_SHOW_LINE_MAX; ++i) {

        if (temp_p_current->item_Index == 0) break;                          // 先判断,看看后面还有没有子节点,没有的话直接退出
        // 显示前5项菜单
        OLED_ShowStr(HIGH_SIGN_OFFSET, i + OLED_SHOW_LINE_START, temp_p_current->disp_String, 1);
        temp_p_current++;                                                 // 有子节点的话将指针指向子节点
    }
}

void total_item() {
    menu *temp_p_current = p_Current_Item;                         // 用临时指针防止污染主指针
    total_current_child_item = 0;                                  // 归零
    // 计算数组大小
    while (temp_p_current->item_Index != 0) {
        temp_p_current++;
        total_current_child_item++;
    }
}